package mp3;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.activation.MimetypesFileTypeMap;

import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.CannotWriteException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.KeyNotFoundException;
import org.jaudiotagger.tag.TagException;

public class ModifyFolder {
	
	private MimetypesFileTypeMap fileType;
	private final String MP3_FILE_TYPE ="application/octet-stream";
	
	public ModifyFolder() {
		fileType = new MimetypesFileTypeMap();
	}

	/**
	 * Clean MP3 only if it is not already clean
	 * ENCODER set to MP3 if it is clean
	 * @param absolutePath
	 * @throws Exception
	 */
	public void cleanMP3File(String absolutePath) throws Exception{
		File testFile = new File(absolutePath);
		ModifyMP3 m = new ModifyMP3(testFile, false);
		m.start();
		if (!m.isClean()) {
			m.setFields();
		}
	}
	
	public void listFilesForFolder(File folder, List<String> allMp3) {
	    for (final File fileEntry : folder.listFiles()) {
	        if (fileEntry.isDirectory()) {
	            listFilesForFolder(fileEntry, allMp3);
	        } else {
//	        	System.out.println(fileType.getContentType(fileEntry));
//	        	System.out.println(extension);
	        	String fileName = fileEntry.getName();
	        	String[] splitter = fileName.split("\\.");
	        	String extension = splitter[splitter.length-1];
	            if(MP3_FILE_TYPE.equals(fileType.getContentType(fileEntry)) 
	            		&& extension.equalsIgnoreCase("mp3")) {
	        		System.out.println(fileEntry.getAbsolutePath());
	        		allMp3.add(fileEntry.getAbsolutePath());
	        	}
	        }
	    }
	}

}
